package com.wzq.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzq.dao.pojo.PageBean;
import com.wzq.dao.pojo.R;
import com.wzq.dao.pojo.Type;
import com.wzq.service.ITypeService;
import com.wzq.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/type")
public class AdminTypeController {

    @Autowired
    ITypeService iTypeService;
    @RequestMapping("/list")
    public R list(@RequestBody PageBean pageBean){
        System.out.println(pageBean);
        String query=pageBean.getQuery().trim();
        Page<Type> page=new Page<>(pageBean.getPageNum(),pageBean.getPageSize());
        Page<Type> pageResult = iTypeService.page(page, new QueryWrapper<Type>().like(StringUtil.isNotEmpty(query), "type", query));
        Map<String,Object> map=new HashMap<>();
        ArrayList<Type> types = new ArrayList<Type>();
        for (Type record : pageResult.getRecords()) {
            if (record.getParent() == 1)        record.setParent_c("活动");
            else                                record.setParent_c("资源");
            types.add(record);
        }
        map.put("TypeList",types);
        map.put("total",pageResult.getTotal());
        return R.ok(map);
    }

    @RequestMapping("/save")
    public R save(@RequestBody Type type){
        if(type.getId()==null || type.getId()==-1){
            iTypeService.save(type);
        }else{
            iTypeService.saveOrUpdate(type);
        }
        return R.ok();
    }

    @GetMapping("/{id}")
    public R findById(@PathVariable(value = "id")Integer id){
        Type type = iTypeService.getById(id);
        Map<String,Object> map=new HashMap<>();
        map.put("type",type);
        return R.ok(map);
    }

    @GetMapping("/delete/{id}")
    public R delete(@PathVariable(value = "id")Integer id){
        iTypeService.removeById(id);
            return R.ok();
        }


    @RequestMapping("/listAll")
    public R listAll(){

        QueryWrapper queryWrapper=new QueryWrapper<Type>();
        queryWrapper.in("id",7,8,9);
        Map<String,Object> map=new HashMap<>();
        map.put("TypeList",iTypeService.list(queryWrapper));
        return R.ok(map);
    }

    @RequestMapping("/listAll1")
    public R listAll1(){
        Map<String,Object> map=new HashMap<>();
        map.put("TypeList",iTypeService.list(new LambdaQueryWrapper<Type>().eq(Type::getParent, 0)));
        return R.ok(map);
    }


}

